package com.topfinance.frame.web;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.Converter;


/**
 * 日期类型和其他类型的转换
 * @author mz
 *
 */
public class DateConverter implements Converter {

    private static SimpleDateFormat df = new SimpleDateFormat();   
    /**  
     * 模式集合.  
     */  
    private static Set<String> patterns = new HashSet<String>();   
    //注册一下日期的转换格式   
    static{   
        DateConverter.patterns.add("yyyy-MM-dd");   
        DateConverter.patterns.add("yyyy-MM-dd HH:mm");   
        DateConverter.patterns.add("yyyy-MM-dd HH:mm:ss");   
        DateConverter.patterns.add("yyyy/MM/dd HH:mm:ss");   
    }   
	
    public Object convert(Class type, Object value) {
        if (value == null) {
			return null;
		} else if (type == Date.class && value instanceof String) {
			return convertToDate(type, value);
		} else if (type == String.class && value instanceof Date) {
			return convertToString(type, value);
		} else if (type == java.sql.Date.class && value instanceof Date) {
			return convertUtilDateToSqlDate(type, value);
		} else if (type == java.util.Date.class
				&& value instanceof java.sql.Date) {
			return convertSqlDateToUtilDate(type, value);
		} else {
			return value;
		}
    }
    
    protected Object convertUtilDateToSqlDate(Class type, Object value) {
    	if(value instanceof java.util.Date){
    		java.sql.Date current = new java.sql.Date(((Date)value).getTime());
    		return current;
    	}
		return value;
	}
    
    protected Object convertSqlDateToUtilDate(Class type, Object value) {
    	if(value instanceof java.sql.Date){
    		
    		return (Date)value;
    	}
		return value;
	}
    
    protected Object convertToDate(Class type,Object value){   
        if(value == null){   
            return null;   
        }else if(value instanceof String){   
            Object dateObj = null;   
            Iterator it = patterns.iterator();   
            while(it.hasNext()){   
                try{   
                    String pattern = (String)it.next();   
                    df.applyPattern(pattern);   
                    dateObj = df.parse((String)value);   
                    break;   
                }catch(Exception pe){
                	//do iterator continue
                }   
            }   
            return dateObj;   
        }else{   
            throw new ConversionException("Could not convert " +
            	      value.getClass().getName() + " to " +
            	      type.getName());
        }   
    }   

    
//    protected Object convertToDate(Class type, Object value) {
//        DateFormat df = new SimpleDateFormat(DateUtil.getDatePattern());
//        if (value instanceof String) {
//            try {
//                if (StringUtils.isEmpty(value.toString())) {
//                    return null;
//                }
//
//                return df.parse((String) value);
//            } catch (Exception pe) {
//                throw new ConversionException("Error converting String to Date");
//            }
//        }
//
//        throw new ConversionException("Could not convert " +
//                                      value.getClass().getName() + " to " +
//                                      type.getName());
//    }

    protected Object convertToString(Class type, Object value) {
        if (value instanceof Date) {
            Object dateObj = null;   
            Iterator it = patterns.iterator();   
            while(it.hasNext()){   
                try{   
                    String pattern = (String)it.next();   
                    df.applyPattern(pattern);   
                    dateObj = df.format(value);   
                    break;   
                }catch(Exception e){
                	//do iterator continue
                }   
            }   
            return dateObj;   
        }

        return value.toString();
    }

//    protected Object convertToString(Class type, Object value) {
//        DateFormat df = new SimpleDateFormat(DateUtil.getDatePattern());
//        if (value instanceof Date) {
//            try {
//                return df.format(value);
//            } catch (Exception e) {
//                throw new ConversionException("Error converting Date to String");
//            }
//        }
//
//        return value.toString();
//    }
}
